package com.wswy.wzcx.ui.other;

import android.content.Context;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by wangduheng26 on 2018/1/10.
 * wangduheng26@gmail.com
 */

public class RecycleItemClickListener extends RecyclerView.SimpleOnItemTouchListener {
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    GestureDetectorCompat mGestureDetector;
    private OnItemClickListener mListener;
    private RecyclerView recyclerView;

    public RecycleItemClickListener(Context context, OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetectorCompat(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                if (recyclerView != null) {
                    View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
                    if (childView != null) {
                        mListener.onItemClick(childView, recyclerView.getChildLayoutPosition(childView));
                        return false;
                    }
                }
                return false;

            }

            @Override
            public void onLongPress(MotionEvent e) {
                super.onLongPress(e);
                if (recyclerView != null) {
                    View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
                    if (childView != null) {
                        mListener.onItemClick(childView, recyclerView.getChildLayoutPosition(childView));
                    }
                }

            }

        });

    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        recyclerView = rv;
        if (/*childView != null && mListener != null &&*/ mGestureDetector.onTouchEvent(e)) {
            return true;
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
        super.onTouchEvent(rv, e);

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
        super.onRequestDisallowInterceptTouchEvent(disallowIntercept);
    }
}
